/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.coupon.service.impl;

import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wealth.coupon.entity.Coupon;
import com.wealth.coupon.entity.CouponRule;
import com.wealth.coupon.enums.CouponModeEnum;
import com.wealth.coupon.enums.CouponStatusEnum;
import com.wealth.coupon.repository.CouponDao;
import com.wealth.coupon.repository.CouponRuleDao;
import com.wealth.coupon.service.ICouponService;
import com.wealth.coupon.service.MakeLabelService;
import com.wealth.coupon.util.LabelFileEncrypt;

import iaas.service.impl.BaseServiceImpl;
import iaas.utils.CommonUtil;
import iaas.utils.ResultObject;

/**
 * @author chan
 */
@Component
public class CouponServiceImpl extends BaseServiceImpl<Coupon> implements ICouponService{

	Logger logger = Logger.getLogger(CouponServiceImpl.class);
	
	@Autowired
	private CouponDao couponDao;
	@Autowired
	private CouponRuleDao couponRuleDao;
	@Autowired
	private MakeLabelService makeLabelService;
	
	/**
	 * 产生四位唯一标识 32机制的字符串
	 */
	private String generateUid() {
		String uid = RandomStringUtils.random(4, LabelFileEncrypt.BASE32);
		Coupon coupon = couponDao.findByUid(uid);
		if(coupon==null){
			return uid;
		}else{
			return generateUid();
		}
	}
	
	/** 制作优惠券文件 */
	@Override
	@Transactional(readOnly=false)
	public boolean createCouponFile(Long batchid) throws Exception{
		//查询
		Coupon coupon = couponDao.findOne(batchid);
		String filepath = "";
		if(coupon.getStatus()==CouponStatusEnum.COMPLETED.getValue()){
			filepath=coupon.getFilepath();
		}else{
			filepath=makeLabelService.createLabel(coupon.getUid(),coupon.getStartno(),coupon.getEndno());
		}
		//生成文件，返回下载路径
		System.out.println("生成的文件路径"+filepath);
		
		if(CommonUtil.isNull(filepath)){
			logger.error("生产批次文件  保存文件路径失败！");
			return false;
		}else{
			coupon.setMakeTime(new Date());
			coupon.setFilepath(filepath);
			coupon.setStatus(CouponStatusEnum.COMPLETED.getValue());
			couponDao.save(coupon);
			return true;
		}
	}
	
	/** 新增优惠券批次 */
	@Override
	public ResultObject saveCoupon(Coupon coupon) throws Exception {
		
		//非纸质模式的优惠券批次，有且只能有一个
		if(coupon.getMode()!=CouponModeEnum.PAPER.getValue()){
			Coupon temp = couponDao.findByMode(coupon.getMode());
			if(temp!=null){
				return new ResultObject("500", "该优惠券模式，有且只有一个", "");
			}
		}
		
		
		//保存优惠券规则
		CouponRule rule = coupon.getRule();
		rule.setCreateTime(new Date());
		couponRuleDao.save(rule);
		
		//保存优惠券批次
		coupon.setUid(generateUid());
		coupon.setRule(rule);
		couponDao.save(coupon);
		
		return new ResultObject("000", "新建优惠券批次成功", "");
	}
	
	
	/** 修改优惠券批次 */
	@Override
	public void updateCoupon(Coupon coupon) throws Exception {
		//保存优惠券规则
		CouponRule rule = coupon.getRule();
		couponRuleDao.save(rule);
		
		//保存优惠券批次
		couponDao.save(coupon);
	}

	@Override
	public Coupon findByMode(Integer mode) throws Exception {
		
		return couponDao.findByMode(mode);
	}

}
